package de.bastian.hinterleitner.database;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import de.bastian.hinterleitner.tools.Category;
import de.bastian.hinterleitner.tools.Entry;
import de.bastian.hinterleitner.tools.Unit;
import de.hijacksoft.androidupdate.DownloadHelper;
import de.hijacksoft.wyc.R;

public class OnlineHelper extends DataHelper {

	public static final String API_URL = "http://basti.hijacksoft.de/wyc/";
	
	public static final String GET = "get.php";
	
	public String username = "06fc67ad11afaadf3021e7d5facbf412";
	public String password = "06fc67ad11afaadf3021e7d5facbf412";
	
	public OnlineHelper(Context context) {
		super(context);
	}

	@Override
	public void onResume() {
		// TODO onResume
	}

	@Override
	public boolean close() {
		// TODO close
		return true;
	}

	@Override
	public void insert(Entry eintrag) {
		// TODO Auto-generated method stub
	}

	@Override
	public void update(Entry eintrag) {
		// TODO Auto-generated method stub
	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
	}

	@Override
	public Entry getEntryById(int id) {
		return new Entry("ERROR");
	}

	@Override
	public ArrayList<Entry> getAllEntries() {
		JSONObject result = getJSONFromPage(API_URL+GET+"?username="+username+"&password="+password);
		try {
			if(result.getInt("id")==3){
				entries =  parseEntries(result.getJSONArray("content"));
				Log.e(TAG,entries.toString());
				return entries;
			}else{
				return new ArrayList<Entry>();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Entry>();
		}
	}

	private ArrayList<Entry> parseEntries(JSONArray array) {
		ArrayList<Entry> result = new ArrayList<Entry>();
		for(int i=0; i<array.length(); i++){
			Entry e = null;
			try {
    			JSONObject obj = array.getJSONObject(i);
    			String name = StringEscapeUtils.unescapeHtml4(obj.getString("name"));
    			double val = obj.getDouble("value");
    			int id = obj.getInt("id");
    			e = new Entry(name, val, id);
    			e.setRepeatableReference(obj.getInt("reference"));
    			String cur = StringEscapeUtils.unescapeHtml4(obj.getString("currency"));
    			e.setUnit(Unit.get(cur, context));
    			String date = obj.getString("date");
    			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN);
    		    Date d =  df.parse(date);
    			e.setDate(d);
    			Category cat = getCategoryByName(obj.getString("categoryname"));
    			e.setCategory(cat);
			} catch (JSONException e1) {
				e1.printStackTrace();
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			result.add(e);
		}
		return result;
	}

	@Override
	public int getNumEntries() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<Entry> getAllEntriesAt(int year, int month, int day) {
		return new ArrayList<Entry>();
	}

	@Override
	protected ArrayList<Entry> getFromDBAllEntriesAt(int year, int month,
			int day) {
		return new ArrayList<Entry>();
	}

	@Override
	public ArrayList<Entry> getAllEntriesContaining(String substring) {
		return new ArrayList<Entry>();
	}

	@Override
	public ArrayList<Entry> getAllEntriesAt(int year, int month) {
		return entries;
	}

	@Override
	public ArrayList<Entry> getAllEntriesAt(int year) {
		return new ArrayList<Entry>();
	}

	@Override
	public ArrayList<Entry> getAllEntriesByCategory(Category c) {
		return new ArrayList<Entry>();
	}

	@Override
	public boolean renameCategory(Category c, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteCategory(Category category) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Category> getAllCategories() {
		return new ArrayList<Category>();
	}

	@Override
	public ArrayList<String> getAllEntryNames() {
		return new ArrayList<String>();
	}

	@Override
	public ArrayList<Integer> getAllYears() {
		return new ArrayList<Integer>();
	}

	@Override
	public ArrayList<Integer> getAllMonths(int year) {
		return new ArrayList<Integer>();
	}

	@Override
	public ArrayList<Integer> getAllDays(int year, int month) {
		return new ArrayList<Integer>();
	}

	@Override
	public Drawable getCategoryDrawable(String category) {
		return context.getResources().getDrawable(R.drawable.trash);
	}

	@Override
	public String getCategoryDrawableName(String category) {
		return "trash.png";
	}

	@Override
	public int getCategoryID(String category) {
		// TODO Auto-generated method stub
		return -1;
	}

	@Override
	public void setCategoryDrawable(String cat, String drawable) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean addCategory(Category cat) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void addTrashCategory() {
		// TODO Auto-generated method stub
	}

	@Override
	protected ArrayList<Entry> getAllRepeatables(int repeatableReference,
			Date date) {
		return new ArrayList<Entry>();
	}

	@Override
	protected int getNewRefID() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<Entry> getAllEntriesGroupedByName() {
		return new ArrayList<Entry>();
	}

	@Override
	public Category getCategoryByName(String name) {
		return new Category("test", "trah.png", -1);
	}
	
	private JSONObject getJSONFromPage(String url){
		
        String result = DownloadHelper.downloadContent(url);
        
        try {            
            return new JSONObject(result);
        } catch (Exception e) {
        	e.printStackTrace();
    		return null;
        }

	}

}
